########## This file produces Figure 3. ###########

library(reshape)
library(ggplot2)
library(extrafont)
#font_import(pattern="GARA")
#Sys.setenv(R_GSCMD = "C:\\Program Files\\gs\\gs9.22\\bin\\gswin32C.exe")

load("Experimental_Output.RData")

#### Please note that the True/False Question number 6 in the raw data file 
#### has been switched with Question number 7 in this file, only to make the
#### resulting figure easier to read.

cum = 100*(c(sum(data1$tfc_sum0),sum(data1$tfc_sum1),sum(data1$tfc_sum2),
             sum(data1$tfc_sum3),sum(data1$tfc_sum4), sum(data1$tfc_sum5),
             sum(data1$tfc_sum7),sum(data1$tfc_sum6),sum(data1$tfc_sum8),
             sum(data1$tfc_sum9))/length(data1$tfc_sum0))


se = function(p,n) sqrt( (p*(1-p)) /length(n) )




df_tf2 <-  data.frame(
  question = as.factor(rep(c(1:9))),
  question_total = round(c(100*c(mean(na.omit(data1$tfc_1)), mean(na.omit(data1$tfc_2)),
                                 mean(na.omit(data1$tfc_3)),mean(na.omit(data1$tfc_4)),
                                 mean(na.omit(data1$tfc_5)),mean(na.omit(data1$tfc_7)),
                                 mean(na.omit(data1$tfc_6)),mean(na.omit(data1$tfc_8)),
                                 mean(na.omit(data1$tfc_9)))),1),
  cum_sum = round(c(sum(cum[2:10]),sum(cum[3:10]),sum(cum[4:10]),sum(cum[5:10]),
                    sum(cum[6:10]),sum(cum[7:10]),
                    sum(cum[8:10]),sum(cum[9:10]),sum(cum[10:10])),1))

df_tf2$question_total2 <- df_tf2$question_total/100
df_tf2$questionSE = 100*c(se(df_tf2$question_total2[1],data1$tfc_1),se(df_tf2$question_total2[2],data1$tfc_2),
                      se(df_tf2$question_total2[3],data1$tfc_3),se(df_tf2$question_total2[4],data1$tfc_4),
                      se(df_tf2$question_total2[5],data1$tfc_5),se(df_tf2$question_total2[6],data1$tfc_6),
                      se(df_tf2$question_total2[7],data1$tfc_7),se(df_tf2$question_total2[8],data1$tfc_8),
                      se(df_tf2$question_total2[9],data1$tfc_9))
                  
ggplot(df_tf2) + 
  geom_bar(aes(x = question, weight = question_total), fill="gray65") +
  geom_errorbar(aes(x = question, ymin=question_total-(1.96*questionSE), ymax=question_total+(1.96*questionSE)),
                size=.3,    # Thinner lines
                width=.2,
                position=position_dodge(.9))+
  coord_cartesian(xlim = c(1,9), ylim = c(-1,110))+
  geom_point(aes(x=as.numeric(question), y= cum_sum), size=3) +
  geom_line(aes(x = as.numeric(question), y = cum_sum), size=1) + 
  geom_text(aes(x = as.numeric(question), y = cum_sum, label=cum_sum),
            hjust=-.2, vjust=-.2,show.legend  = F,size=4,fontface = "bold", family="Garamond") + 
  geom_text(aes(x = as.numeric(question), y = question_total, label=question_total),
            hjust=.5, vjust=2,show.legend  = F,size=4,color="white",fontface = "bold", family="Garamond") +
  xlab("\n Question Number") +
  ylab("Percentage Respondents\n") +
  theme_bw() + theme(text=element_text(family="Garamond"))
ggsave(file="BonillaMo_Figure3.jpg", 
       width = 250, height = 125, units = "mm", dpi=600)

